using System;
using System.Collections.Generic;
using System.Linq;
//FOR WINDOWS AND DEBUG ONLY-------------------------------------------------------------
////using System.Runtime.InteropServices;
//---------------------------------------------------------------------------------------
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TomShane.Neoforce.Controls;

namespace SpaceInvade
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Application
    {
//------------------------------------------------------------------------------------------------------
        //////FOR WINDOWS AND DEBUGGING ONLY. ALLOWS USE OF MESSAGEBOX
        ////[DllImport("user32.dll", CharSet = CharSet.Auto)]
        ////public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);
//------------------------------------------------------------------------------------------------------
        public static Texture2D pixel = null;
        public static GameTime Time;
        StateManager STManager;
        

        public Game1() : base(false)
        {
            Manager.Content.RootDirectory = "Content";
            Manager.SkinDirectory = @"Content\Skins\";
            ExitConfirmation = false;
            Manager.Graphics.PreferredBackBufferWidth = 640;
            Manager.Graphics.PreferredBackBufferHeight = 480;
            //Manager.Graphics.IsFullScreen = true;
            IsFixedTimeStep = true;
        }

        protected override void Initialize()
        {
            base.Initialize();
            LevelReader.InitLevelReader(Manager);
            Sound.InitSound(Manager);
            STManager = new StateManager();
            StateManager.SelectMode(new StartMenu(Manager));
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            pixel = new Texture2D(GraphicsDevice, 1, 1);
            pixel.SetData(new[] { Color.White });
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            Time = gameTime;
            // Allows the game to exit
            KeyboardState CurrentKeyState = Keyboard.GetState();
            if (CurrentKeyState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            StateManager.Update();
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            // TODO: Add your drawing code here
            StateManager.Draw();
            base.Draw(gameTime);
        }
        protected override void Dispose(bool disposing)
        {
            Manager.GraphicsDevice.SetRenderTarget(null);
            StateManager.Dispose();
            Sound.Dispose();
            base.Dispose(disposing);
        }
        public override void Exit()
        {
            // nulling the render target seemed to fix the AccessViolationException
            Manager.GraphicsDevice.SetRenderTarget(null);
            StateManager.Dispose();
            Sound.Dispose();
            base.Exit();
        }
    }
}
